package com.lj.universalframework.widget

import android.content.Context
import android.text.TextUtils
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import com.lj.universalframework.R
import com.lj.universalframework.app.MyApplication

/**
 * <p>文件名：ToastShow
 * <p>创建者：LJ
 * <p>创建日期：2020\10\19 0019 10:40
 * <p>描述：自定义吐司
 */
class ToastShow {
    companion object {

        //成功
        fun success(context: Context, content: String) {
            if (TextUtils.isEmpty(content)) return
            try {
                var toast = Toast(context)
                val views: View = LayoutInflater.from(context).inflate(R.layout.toast, null)

                val tvToast = views.findViewById<TextView>(R.id.tv_toast)
                tvToast.text = content
                toast.setGravity(Gravity.BOTTOM, 0, 300)
                // 加载自定义布局
                toast.view = views
                toast.show()
            } catch (e: Exception) {
                e.message
            }
        }

        //错误
        fun err(context: Context=MyApplication.mContext, content: String) {
            if (TextUtils.isEmpty(content)) return
            try {
                var toast = Toast(context)
                val views: View = LayoutInflater.from(context).inflate(R.layout.toast, null)

                val llToast = views.findViewById<LinearLayout>(R.id.ll_toast)
                llToast.setBackgroundColor(context.resources.getColor(R.color.err_bg))

                val ivToast = views.findViewById<ImageView>(R.id.iv_toast)
                ivToast.setImageResource(R.mipmap.err_toast)

                val tvToast = views.findViewById<TextView>(R.id.tv_toast)
                tvToast.text = content
                tvToast.setTextColor(context.resources.getColor(R.color.err_tv))
                toast.setGravity(Gravity.BOTTOM, 0, 300)
                // 加载自定义布局
                toast.view = views
                toast.show()
            } catch (e: Exception) {
                e.message
            }
        }

        //警告
        fun warn(context: Context, content: String) {
            if (TextUtils.isEmpty(content)) return
            try {
                var toast = Toast(context)
                val views: View = LayoutInflater.from(context).inflate(R.layout.toast, null)

                val llToast = views.findViewById<LinearLayout>(R.id.ll_toast)
                llToast.setBackgroundColor(context.resources.getColor(R.color.warn_bg))

                val ivToast = views.findViewById<ImageView>(R.id.iv_toast)
                ivToast.setImageResource(R.mipmap.warn_toast)

                val tvToast = views.findViewById<TextView>(R.id.tv_toast)
                tvToast.text = content
                tvToast.setTextColor(context.resources.getColor(R.color.warn_tv))
                toast.setGravity(Gravity.BOTTOM, 0, 300)
                // 加载自定义布局
                toast.view = views
                toast.show()
            } catch (e: Exception) {
                e.message
            }
        }
    }
}